import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @version 1.0
 * @autho 3505
 * @date 2018/5/5
 */
public class TestReceive implements Runnable{


    @Override
    public void run() {
        DatagramSocket datagramSocket=null;
        try {
            datagramSocket=new DatagramSocket(9000);
        }catch (Exception E){
            E.printStackTrace();
        }
        byte[] by=new byte[1024];
        DatagramPacket datagramPacket=new DatagramPacket(by,by.length);
        System.out.println("等待接收数据...");
        while (true) {
            try {
                datagramSocket.receive(datagramPacket);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String str = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
            System.out.println(str + "-->发送主机地址" + datagramPacket.getAddress().getHostAddress() );
//            System.out.println(str + "-->发送主机地址" + datagramPacket.getAddress().getHostAddress() + " : 端口号" + datagramPacket.getPort());
        }
    }
}

